package com.proyecto.robomind.model;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

/**
 * @author impaler
 *
 */
public class Ninja {
	
	private static final String TAG = Ninja.class.getSimpleName();

	private static final int ORIENTATION_NORTH = 3;
	private static final int ORIENTATION_EAST = 1;
	private static final int ORIENTATION_WEST = 2;
	private static final int ORIENTATION_SOUTH = 0;
	
	private boolean goingForward;
	private int stepsLeft;
	
	private Bitmap bitmap;		// the animation sequence
	private Rect sourceRect;	// the rectangle to be drawn from the animation bitmap
	
	private int orientation; 	//character orientation
	private int frameNr;		// number of frames in animation
	private int currentFrame;	// the current frame
	private long frameTicker;	// the time of the last frame update
	private int framePeriod;	// milliseconds between each frame (1000/fps)
	
	private int spriteWidth;	// the width of the sprite to calculate the cut out rectangle
	private int spriteHeight;	// the height of the sprite
	
	private int x;				// the X coordinate of the object (top left of the image)
	private int y;				// the Y coordinate of the object (top left of the image)
	
	private MapSquare obj;
	private int parentWidth;
	private int parentHeight;
	
	public Ninja(Bitmap bitmap, int x, int y, int parentHeight, int parentWidth,int fps, int frameCount) {
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
		currentFrame = 0;
		frameNr = frameCount;
		spriteWidth = bitmap.getWidth() / frameCount;
		spriteHeight = bitmap.getHeight() / 4;
		sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
		framePeriod = 1000 / fps;
		frameTicker = 0l;
		this.parentHeight = parentHeight;
		this.parentWidth = parentWidth;
		orientation = ORIENTATION_SOUTH;
		goingForward = false;
		stepsLeft = 0;
	}
	
	public Bitmap getBitmap() {
		return bitmap;
	}
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}
	public Rect getSourceRect() {
		return sourceRect;
	}
	public void setSourceRect(Rect sourceRect) {
		this.sourceRect = sourceRect;
	}
	public int getFrameNr() {
		return frameNr;
	}
	public void setFrameNr(int frameNr) {
		this.frameNr = frameNr;
	}
	public int getCurrentFrame() {
		return currentFrame;
	}
	public void setCurrentFrame(int currentFrame) {
		this.currentFrame = currentFrame;
	}
	public int getFramePeriod() {
		return framePeriod;
	}
	public void setFramePeriod(int framePeriod) {
		this.framePeriod = framePeriod;
	}
	public int getSpriteWidth() {
		return spriteWidth;
	}
	public void setSpriteWidth(int spriteWidth) {
		this.spriteWidth = spriteWidth;
	}
	public int getSpriteHeight() {
		return spriteHeight;
	}
	public void setSpriteHeight(int spriteHeight) {
		this.spriteHeight = spriteHeight;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getOrientation(){
		return orientation;
	}
	public void setOrientation(int orientation){
		this.orientation = orientation;
	}
	public boolean hasObj(){
		if(obj!=null){
			return true;
		}else{
			return false;	
		}
	}
	public boolean isGoingForward(){
		return goingForward;
	}
	
	public void goRight(int x){
		for(int i = 0; i < x;i++){
			switch(orientation){
			case ORIENTATION_NORTH:
				orientation = ORIENTATION_EAST;
				break;
			case ORIENTATION_SOUTH:
				orientation = ORIENTATION_WEST;
				break;
			case ORIENTATION_WEST:
				orientation = ORIENTATION_NORTH;
				break;
			case ORIENTATION_EAST:
				orientation = ORIENTATION_SOUTH;
				break;
			}
		}
		currentFrame = 0;
	}
	
	public void goLeft(int x){
		for(int i = 0; i < x;i++){
			switch(orientation){
			case ORIENTATION_NORTH:
				orientation = ORIENTATION_WEST;
				break;
			case ORIENTATION_SOUTH:
				orientation = ORIENTATION_EAST;
				break;
			case ORIENTATION_WEST:
				orientation = ORIENTATION_SOUTH;
				break;
			case ORIENTATION_EAST:
				orientation = ORIENTATION_NORTH;
				break;
			}
		}
		currentFrame = 0;
	}
	
	public void goForward(int x){
		goingForward = true;
		stepsLeft += x;
	}
	
	private void goForward(){
		if(stepsLeft>0){
			switch(orientation){
			case ORIENTATION_NORTH:
				setY(getY()-spriteHeight/frameNr);
				break;
			case ORIENTATION_SOUTH:
				setY(getY()+spriteHeight/frameNr);
				break;
			case ORIENTATION_WEST:
				setX(getX()+spriteWidth/frameNr);
				break;
			case ORIENTATION_EAST:
				setX(getX()-spriteWidth/frameNr);
				break;
			}
			stepsLeft--;
		}else{
			goingForward = false;
		}
	}
	
	public void get(MapSquare obj){
		this.obj = obj;
	}
	
	public MapSquare push(){
		MapSquare obj2 = obj;
		obj = null;
		return obj2;
	}
	
	// the update method for Ninja
	public void update(long gameTime) {
		if (gameTime > frameTicker + framePeriod) {
			frameTicker = gameTime;
			
			currentFrame++;
			if (currentFrame >= frameNr) {
				currentFrame = 0;
			}
		}
		// define the rectangle to cut out sprite
		this.sourceRect.left = currentFrame * spriteWidth;
		this.sourceRect.right = this.sourceRect.left + spriteWidth;
		this.sourceRect.top = orientation*spriteHeight;
		this.sourceRect.bottom = this.sourceRect.top + spriteHeight;
		
		if(isGoingForward()){
			goForward();
		}
	}
	
	// the draw method which draws the corresponding frame
	public void draw(Canvas canvas) {
		// where to draw the sprite
		Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight);
		canvas.drawBitmap(bitmap, sourceRect, destRect, null);	}
}